                                               
DEFINE VARIABLE prate AS DECIMAL.
DEFINE VARIABLE prate2 AS DECIMAL.
DEFINE VARIABLE perror AS CHARACTER.     
DEFINE VARIABLE rnumber AS INT.
DEFINE VARIABLE avgprice LIKE pi_list_price.
DEFINE VARIABLE piprice LIKE pi_list_price.
DEFINE VARIABLE cmsort LIKE cm_sort.
DEFINE VARIABLE cmsort1 LIKE cm_sort.
DEFINE VARIABLE mm AS CHARACTER FORMAT 'x(10)'.
DEFINE VARIABLE aa LIKE tr_price.
DEFINE VARIABLE bb LIKE tr_price.
DEFINE VARIABLE cc LIKE tr_price.
DEFINE VARIABLE a1 AS INT.
DEFINE VARIABLE a2 AS INT.
DEFINE VARIABLE a3 AS INT.
DEFINE VARIABLE sumprice LIKE tr_price.

DEFINE TEMP-TABLE pimstr
    FIELD p_part LIKE wo_part
    FIELD p_mm  AS CHARACTER
    FIELD p_custid LIKE pi_cs_code
    FIELD p_site LIKE si_site
    FIELD p_eprice LIKE tr_price
    FIELD p_aprice LIKE tr_price
    FIELD p_addname AS CHARACTER
    FIELD p_custid1 AS CHARACTER
    FIELD p_addname1 AS CHARACTER
    FIELD p_startdate LIKE pi_start
    FIELD p_ptdesc1 LIKE pt_desc1
    FIELD p_ptdesc2 LIKE pt_desc2 
    FIELD p_site1   LIKE si_site
    INDEX p_part p_part p_site p_custid.

DEF BUFFER b_pimstr FOR pimstr.

/* {mfdeclre.i} */

mm = STRING(YEAR(TODAY),'9999') + 'M' + STRING(MONTH(TODAY),'99').

EMPTY TEMP-TABLE pimstr.


FOR EACH pi_mstr WHERE (pi_start <= TODAY OR pi_start = ?) AND (pi_expire >= TODAY OR pi_expire = ?) 
    BREAK BY pi_user1 BY pi_part_code BY pi_cs_code BY pi_start :
    IF LAST-OF(pi_cs_code) THEN DO:
        ASSIGN avgprice = 0 rnumber = 0 piprice = 0  cmsort = '' cmsort1 = '' prate2 = 1.
   
        for each exr_rate no-lock where exr_curr1 = pi_curr and exr_curr2 = "RMB" and (exr_start_date = ? or exr_start_date <= TODAY /*pi_start*/ )
            and (exr_end_date = ? or exr_end_date >= TODAY /*pi_start*/ ):
            pRate2 = exr_rate2.
        end.

        if pRate2 = 1 then do:
            for each exr_rate no-lock where exr_curr1 = "RMB" and exr_curr2 = pi_curr and (exr_start_date = ? or exr_start_date <= TODAY /*pi_start*/ )
                and (exr_end_date = ? or exr_end_date >= TODAY /*pi_start*/ ):
                pRate2 = exr_rate.
            end.
        end.
/*             {gprunp.i "mcpl" "p" "mc-get-ex-rate" */
/*              "(input pi_curr,                     */
/*               input 'rmb',                        */
/*               input """",                         */
/*               input pi_start,                     */
/*               output prate,                       */
/*               output prate2,                      */
/*               output perror)"}.                   */

               
/*  FOR FIRST exr_rate WHERE exr_curr1 = 'RMB'           */
/*     AND exr_curr2 = pi_curr AND exr_start >= pi_start */
/*     AND exr_end <= pi_start:                          */
/*     prate2 = exr_rate.                                */
/*   END.                                                */


        FIND FIRST pt_mstr WHERE pt_part = pi_part_code  NO-LOCK NO-ERROR.
        IF AVAILABLE pt_mstr THEN DO:
            IF  CAN-FIND(FIRST code_mstr WHERE code_fldname = "pt_prod_line" AND code_value = pt_prod_line AND code_cmmt = "10") THEN DO:
                
                FIND FIRST cm_mstr WHERE cm_addr = pi_cs_code NO-LOCK NO-ERROR.
                IF AVAILABLE cm_mstr THEN 
                    ASSIGN cmsort1 = cm_sort cmsort = SUBSTRING(cm_sort,1,INDEX(cm_sort,'-') - 1).

                FIND FIRST CODE_mstr NO-LOCK WHERE CODE_fldname = "s_dept" AND CODE_value = pi_user1 NO-ERROR.

                CREATE pimstr.
                ASSIGN 
                    p_part = pi_part_code
                    p_mm   = mm
                    p_custid = IF pi_cs_code BEGINS "qadall" THEN "" ELSE SUBSTRING(pi_cs_code,1,5)
                    p_site = /*SUBSTRING(pi_user1,1,2) + '0'*/ IF AVAILABLE CODE_mstr THEN CODE_cmmt ELSE ""
                    p_eprice = pi_list_price * prate2
                    p_aprice = pi_list_price * prate2
                    p_addname = cmsort
                    p_custid1 = pi_cs_code
                    p_addname1 = cmsort1
                    p_startdate = IF pi_start <> ? THEN pi_start ELSE TODAY
                    p_ptdesc1 = pt_desc1
                    p_ptdesc2 = pt_desc2
                    p_site1 = pi_user1.
            END.
        END.
    END.
END.                                                                            

FOR EACH pimstr EXCLUSIVE-LOCK WHERE p_custid = 'CA001' BREAK BY p_part :
    IF FIRST-OF(p_part) THEN DO:
        avgprice = 0.
       
        FOR EACH b_pimstr WHERE b_pimstr.p_part = pimstr.p_part AND b_pimstr.p_custid = 'CA001' BREAK BY b_pimstr.p_site BY b_pimstr.p_custid1:
            IF FIRST-OF(b_pimstr.p_site) THEN 
                ASSIGN aa = 0 bb = 0 cc = 0 rnumber = 0 sumprice = 0 a1 = 0 a2 = 0 a3 = 0 avgprice = 0.
           
            ASSIGN sumprice = sumprice + p_eprice rnumber = rnumber + 1.
            
            IF SUBSTRING(p_custid1,INDEX(p_custid1,'-') + 1,1) = 'A' OR SUBSTRING(p_custid1,INDEX(p_custid1,'-') + 1,1) = 'B' THEN
                ASSIGN aa = aa + p_eprice * 0.4 a1 = a1 + 1.
            ELSE IF SUBSTRING(p_custid1,INDEX(p_custid1,'-') + 1,1) = 'C' OR SUBSTRING(p_custid1,INDEX(p_custid1,'-') + 1,1) = 'D' THEN
                ASSIGN bb = bb + p_eprice * 0.4 a2 = a2 + 1.
            ELSE IF SUBSTRING(p_custid1,INDEX(p_custid1,'-') + 1,1) = 'E' OR SUBSTRING(p_custid1,INDEX(p_custid1,'-') + 1,1) = 'F' THEN
                ASSIGN cc = cc + p_eprice * 0.2 a3 = a3 + 1.
     
            IF LAST-OF(b_pimstr.p_site) THEN DO:
                IF cc = 0 OR bb = 0 OR aa = 0 THEN 
                    avgprice = sumprice / rnumber.
                ELSE 
                    avgprice = aa / a1 + bb / a2 + cc / a3.
            END.
        END.
    END.

    ASSIGN pimstr.p_aprice = avgprice.  
END.

OUTPUT TO VALUE('\\192.168.12.12\ftp$\csv\R1001B\pimstr.csv').

EXPORT DELIMITER "," 'DB' 'Part' 'mm' 'CustID' 'Site' 'qprice' 'addname' 'CustID1' 'addname1' 'Startdate' 'Ptdesc1' 'Ptdesc2' 'Site1' 'eprice' .

FOR EACH pimstr:
    EXPORT DELIMITER "," 'HK' p_part p_mm p_custid p_site  p_aprice p_addname p_custid1 p_addname1 p_startdate p_ptdesc1 p_ptdesc2 p_site1 p_eprice.
END.

QUIT.
 
